#include "SPPlugin.h"
#include "Log.h"
#include "Expand/PluginsManager.h"
#include "Game/IInventory.hpp"
#include "Managers/GameManager.h"
#include "Components/ComponentInventory.h"
#include "Components/ComponentPlayer.h"
#include "Components/ComponentBody.h"

#include "Network/Client.h"
#include "Player/PlayerData.h"

#include "Subsystems/SubsystemPlayers.h"

#include "ServerConfigure.h"
#include "GameSingleton.h"

#include "LiteNetLib/NetConstants.h"

namespace CXHGT
{
    static const std::string titleText = "设出生点";
    constexpr const char *help1 = "\n"
                                  "/sp    -回到出生点的位置\n"
                                  "/sp set    -设置出生点的位置\n";

    static const std::string normalHelp = help1;

    const std::string &SPPlugin::getPluginName()
    {
        static const std::string pluginName = "回初始出生点插件";
        return pluginName;
    }

    const std::string &SPPlugin::getIntroduce()
    {
        static const std::string introduce = "\n/sp - 回到初始出生点";
        return introduce;
    }

    uint32_t SPPlugin::getUseAuthLevel()
    {
        return 0;
    }

    AuthDisplayType SPPlugin::getDisplayType()
    {
        return AuthDisplayType::Authority;
    }

    void SPPlugin::processCmd(ChatObj &chatObj, std::vector<std::string> &cmdDataVec)
    {
        if(chatObj.m_isTerminal == true)
        {
            chatObj.sendMessage(titleText, "控制台不能使用");
            return;
        }
        if (cmdDataVec.size() == 1)
        {
            PlayerDataPtr playerData = chatObj.m_client->playerData;
            playerData->componentPlayer->setNetPosition(playerData->spawnPosition);
            return;
        }
        else if (cmdDataVec.size() == 2 && cmdDataVec[1] == "set")
        {
            Vector3 position;
            PlayerDataPtr playerData = chatObj.m_client->playerData;
            playerData->componentPlayer->componentBody->getPosition(position);
            playerData->spawnPosition = position;
            chatObj.sendMessage(titleText, fmt::format("设置出生点 ({}) 成功", position.toString()));
            return;
        }
        chatObj.sendMessage(titleText, normalHelp);
    }

    void SPPlugin::getCmds(std::vector<std::string> &cmdVec)
    {
        cmdVec.push_back("/sp");
    }

    void SPPlugin::load() {}

} // namespace PocketSurvival


using namespace PocketSurvival;

extern "C" void initialize()
{
    GameSingleton::singletonPtr->getSingleton<PluginsManager>()->registerPlugin<CXHGT::SPPlugin>();
    Log::Info("成功加载 SPPlugin 插件");
}